<template>
	<xn-form-container
		:title="formData.id ? '编辑会议室类型时间段' : '增加会议室类型时间段'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="时间段：" name="name">
						<a-input v-model:value="formData.name" placeholder="请输入时间段如：9:00-10:00" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="所属机构：" name="orgId">
						<a-select
							v-model:value="formData.orgId"
							class="xn-wd"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择所属机构"
							allow-clear
							:options="orgOptions"
							@change="onOrgChange"
						></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="12">
<!--					<a-form-item label="会议室类型名称：" name="meetingId">-->
<!--						<a-select-->
<!--							v-model:value="formData.devicetypeId"-->
<!--							class="xn-wd"-->
<!--							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"-->
<!--							allow-clear-->
<!--							placeholder="请选择会议室类型名称"-->
<!--							:options="currentTypeOptions"-->
<!--							:disabled="!formData.orgId"-->
<!--						></a-select>-->
<!--					</a-form-item>-->
					<a-form-item label="会议室类型：" name="meetingId">
						<a-input v-model:value="formData.meetingId" placeholder="请输入会议室类型" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="状态：" name="disable">
						<a-select v-model:value="formData.disable" placeholder="请选择是否禁用" :options="disableOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="排序码：" name="sortCode">
						<a-input v-model:value="formData.sortCode" placeholder="请输入排序码" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="meetingTimeForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import meetingTimeApi from '@/api/biz/meetingTimeApi'
	import bizUserApi from "@/api/biz/bizUserApi";
	// import meetingTypeApi from "@/api/biz/meetingTypeApi";
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const disableOptions = ref([])

	const orgTreeData = ref([])
	const treeDefaultExpandedKeys = ref([])
	const meetingTypeTreeDate = ref([])
	const meetingTypeTreeDefaultExpandedKeys = ref([])

	//机构选项
	const orgOptions = ref([])
	const currentTypeOptions = ref([])
	//当前会议室类型选项

	// 获取机构树数据
	const getOrgTree = () => {
		bizUserApi.userOrgTreeSelector().then((res) => {
			if (res !== null) {
				orgTreeData.value = res
				// 默认展开前两级
				orgTreeData.value.forEach((item) => {
					if (item.parentId === '0') {
						treeDefaultExpandedKeys.value.push(item.id)
						if (item.children) {
							item.children.forEach((child) => {
								treeDefaultExpandedKeys.value.push(child.id)
							})
						}
					}
				})
			}
		})
	}

	//获取所有会议室类型
	// const getMeetingTypeTree = () => {
	// 	meetingTypeApi.getAll().then((res) => {
	// 		if (res !== null) {
	// 			meetingTypeTreeDate.value = res
	// 			meetingTypeTreeDefaultExpandedKeys.value = res.filter((item) => item.isOrg).map((item) => item.id)
	//
	// 			orgOptions.value = res
	// 				.filter((item) => item.isOrg)
	// 				.map((item) => ({
	// 					label: item.name,
	// 					value: item.id
	// 				}))
	// 		}
	// 	})
	// }

	const onOrgChange = (orgId) => {
		formData.value.meetingtypeId = undefined

		if (orgId) {
			const orgNode = meetingTypeTreeDate.value.find((item) => item.id === orgId && item.isOrg)
			if (orgNode && orgNode.children) {
				currentTypeOptions.value = orgNode.children.map((child) => ({
					label: child.name,
					value: child.id
				}))
			} else {
				currentTypeOptions.value = []
			}
		} else {
			currentTypeOptions.value = []
		}
	}

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		getOrgTree()
		getMeetingTypeTree()
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		disableOptions.value = tool.dictList('COMMON_STATUS')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		name: [required('请输入时间段')],
		orgId: [required('请输入所属机构')],
		meetingId: [required('请输入会议室类型')],
		disable: [required('请输入是否禁用')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				meetingTimeApi
					.meetingTimeSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
